package com.xgr.wonderful.db;

import java.util.HashMap;
import java.util.Map;

import com.xgr.wonderful.constant.Constant;
import com.xgr.wonderful.entity.Photo;
import com.xgr.wonderful.entity.SuperConversation;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * DAO层一般有接口和这个接口的实现 ，接口用于规范实现类，实现类用于操作数据库。
 * (data access objects)
 * DBOpenHelper--->ConversationDao---->DatabaseUtils 
 * 两个方法：getConversationList,saveConversationList
 *
 */
public class ConversationDao {

	private static final String CONVERSATION_TABLE_QUERY = "select * from "
			+ Constant.CONVERSATION_TABLE_NAME;

	private DBOpenHelper mDbOpenHelper;

	public ConversationDao(Context context) {
		mDbOpenHelper = DBOpenHelper.getInstance(context);
	}
	/**
	 * 查询得到所有 的SuperConversations 
	 * @return
	 */
	public Map<Integer, SuperConversation> getConversationList() {
		SQLiteDatabase db = mDbOpenHelper.getReadableDatabase();
		Map<Integer, SuperConversation> conversations = new HashMap<Integer, SuperConversation>();
		if (db.isOpen()) {
			Cursor cursor = db.rawQuery(CONVERSATION_TABLE_QUERY, null);
			while (cursor.moveToNext()) {
				Integer remoteUserid = cursor.getInt(cursor
						.getColumnIndex(Constant.CONVERSATION_REMOTE_USER_ID));
				String remoteUsername = cursor
						.getString(cursor
								.getColumnIndex(Constant.CONVERSATION_REMOTE_USER_NAME));
				String remoteUser_bigurl = cursor
						.getString(cursor
								.getColumnIndex(Constant.CONVERSATION_REMOTE_USER_PHOTO_BIG_URL));
				String remoteUser_smallurl = cursor
						.getString(cursor
								.getColumnIndex(Constant.CONVERSATION_REMOTE_USER_PHOTO_SMALL_URL));
				Photo remoteUserphoto = new Photo(remoteUser_bigurl,
						remoteUser_smallurl);
				SuperConversation conversation = new SuperConversation(
						remoteUserid, remoteUsername, remoteUserphoto);
				conversations.put(remoteUserid, conversation);
			}
			cursor.close();
		}
		return conversations;
	}

	/**
	 *  有待进一步优化,使用的先删除 再保存的方法
	 * @param conversations
	 */
	public void saveConversationList(
			Map<Integer, SuperConversation> conversations) {
		SQLiteDatabase db = mDbOpenHelper.getWritableDatabase();
		if (db.isOpen()) {
			// method for deleting rows in the database.
			db.delete(Constant.CONVERSATION_TABLE_NAME, null, null);
			for (SuperConversation superConversation : conversations.values()) {
				ContentValues values = new ContentValues();
				values.put(Constant.CONVERSATION_REMOTE_USER_ID,
						superConversation.getRemoteUserId());
				values.put(Constant.CONVERSATION_REMOTE_USER_NAME,
						superConversation.getRemoteUsername());
				//如果 头像为空怎么办
				values.put(Constant.CONVERSATION_REMOTE_USER_PHOTO_BIG_URL,
						superConversation.getRemoteUserPhoto()!= null? 
								superConversation.getRemoteUserPhoto().getUrl(): null);
				values.put(Constant.CONVERSATION_REMOTE_USER_PHOTO_SMALL_URL,
						superConversation.getRemoteUserPhoto()!= null ?
								superConversation.getRemoteUserPhoto().getSmall(): null);
				db.replace(Constant.CONVERSATION_TABLE_NAME, null, values);
			}
		}
	}
}
